﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Htss.Platform.Data
{
    public class DbProc
    {
        //
        List<DbFields> gFields = new List<DbFields>();
        //
        DbFields Field = null;
        private String ProcName;
        public DbProc(string procName)
        {
            this.ProcName = procName;
        }
        /// <summary>
        /// 设置字段
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void SetFields(string key, object value)
        {
            Field = new DbFields();
            //
            Field.Name = key;
            Field.Value = value;
            gFields.Add(Field);
        }
        /// <summary>
        /// 设置字段
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="DbFieldType"></param>
        public void SetFields(string key, object value, DbFieldType DbFieldType)
        {
            Field = new DbFields();
            //
            Field.Name = key;
            Field.Value = value;
            Field.DbFieldType = DbFieldType;
            gFields.Add(Field);
        }
        /// <summary>
        /// 设置字段
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="sqldbType"></param>
        /// <param name="Size"></param>
        public void SetFields(string key, object value, SqlDbType sqldbType, int Size)
        {
            Field = new DbFields();
            //
            Field.Name = key;
            Field.Value = value;
            Field.SqlDbType = sqldbType;
            Field.Size = Size;
            gFields.Add(Field);
        }
        /// <summary>
        /// 设置字段
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="sqldbType"></param>
        /// <param name="Size"></param>
        /// <param name="DbFieldType"></param>
        public void SetFields(string key, object value, SqlDbType sqldbType, int Size, DbFieldType DbFieldType)
        {
            Field = new DbFields();
            //
            Field.Name = key;
            Field.Value = value;
            Field.SqlDbType = sqldbType;
            Field.Size = Size;
            Field.DbFieldType = DbFieldType;
            gFields.Add(Field);
        }
        /// <summary>
        /// 设置字段
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="sqldbType"></param>
        /// <param name="Size"></param>
        /// <param name="parameterDirection"></param>
        public void SetFields(string key, object value, SqlDbType sqldbType, int Size, ParameterDirection parameterDirection)
        {
            Field = new DbFields();
            //
            Field.Name = key;
            Field.Value = value;
            Field.SqlDbType = sqldbType;
            Field.Size = Size;
            Field.Direction = parameterDirection;
            gFields.Add(Field);
        }
        /// <summary>
        /// 设置字段
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="sqldbType"></param>
        /// <param name="Size"></param>
        /// <param name="parameterDirection"></param>
        /// <param name="DbFieldType"></param>
        public void SetFields(string key, object value, SqlDbType sqldbType, int Size, ParameterDirection parameterDirection,DbFieldType DbFieldType)
        {
            Field = new DbFields();
            //
            Field.Name = key;
            Field.Value = value;
            Field.SqlDbType = sqldbType;
            Field.Size = Size;
            Field.Direction = parameterDirection;
            Field.DbFieldType = DbFieldType;
            gFields.Add(Field);
        }
        /// <summary>
        /// 执行存储过程,并且返回存储过程的返回参数值
        /// </summary>
        /// <returns></returns>
        public List<Object> ExecNonQueryList()
        {
            DataAccess g = new DataAccess();
            return g.ExecProcedureNonQueryList(ProcName, GetParameters());
        }
        /// <summary>
        /// 执行存储过程,并且返回存储过程的返回参数值
        /// </summary>
        /// <returns></returns>
        public GHashTable ExecNonQueryHashTable()
        {
            DataAccess g = new DataAccess();
            return g.ExecuteProcedureNonQueryHashTable(ProcName, GetParameters());
        }
        /// <summary>
        /// 执行存储过程,并且返回是或否
        /// </summary>
        /// <returns></returns>
        public bool ExecNonQuery()
        {
            DataAccess g = new DataAccess();
            return g.ExecuteProcedureNonQuery(ProcName, GetParameters()) > 0 ? true : false;
        }
        /// <summary>
        /// 基于存储过程的数据读取
        /// </summary>
        /// <returns></returns>
        public DbTable ExecForReader()
        {
            
            DataAccess da = new DataAccess();

            return da.ExecuteProcedureForReader(ProcName, GetParameters());
        }
        /// <summary>
        /// 执行存储过程,并且返回一个DataSet
        /// </summary>
        /// <returns></returns>
        public DataSet ExecDataSet()
        {
            DataAccess g = new DataAccess();
            return g.ExecuteProcedure(ProcName,GetParameters());
        }
        /// <summary>
        /// 获取到字段设置信息
        /// </summary>
        /// <returns></returns>
        private List<SqlParameter> GetParameters()
        {
            List<SqlParameter> list = new List<SqlParameter>();
            SqlParameter parameter;
            foreach (DbFields f in gFields)
            {
                if (f.DbFieldType != DbFieldType.Query)
                {
                    parameter = new SqlParameter();
                    parameter.ParameterName = f.Name;
                    parameter.Value = f.Value;
                    if (f.SqlDbType != 0)
                    {
                        parameter.SqlDbType = f.SqlDbType;
                    }
                    if (f.Size != 0)
                    {
                        parameter.Size = f.Size;
                    }
                    if (f.Direction != 0)
                    {
                        parameter.Direction = f.Direction;
                    }
                    list.Add(parameter);
                }
            }
            return list;
        }
    }
}
